package com.dkakunsi.monev.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ContextLoader {
	private static ApplicationContext appContext;
	
	public static enum Bean {
		LOGIN_SERVICE,
		PROGRAM_SERVICE,
		KEGIATAN_SERVICE,
		INDIKATOR_SERVICE,
		REALISASI_SERVICE,
		MONITORING_SERVICE,
		REPORT_SERVICE
	}
	
	static {
		if(appContext == null)
			appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
	}

	public static void init() {
		getAppContext();
	}
	
	public static ApplicationContext getAppContext() {
		return appContext;
	}

	public static void setAppContext(ApplicationContext context) {
		appContext = context;
	}
	
	public static Object getBean(String name) {
		return getAppContext().getBean(name);
	}
	
	public static Object getBean(Bean bean) {
		return getBean(getBeanName(bean));
	}
	
	public static String getBeanName(Bean bean) {
		switch(bean) {
			case LOGIN_SERVICE: return "loginService";
			case PROGRAM_SERVICE: return "programService";
			case KEGIATAN_SERVICE: return "kegiatanService";
			case INDIKATOR_SERVICE: return "indikatorService";
			case REALISASI_SERVICE: return "realisasiService";
			case MONITORING_SERVICE: return "monitoringService";
			case REPORT_SERVICE: return "reportService";
			default: return "";
		}
	}
}
